<?php
/**
* @link http://framework.org/
* @copyright Copyright: 2003-2004 Doutromundo
* @author Gregory Brown <bugzbrown@gmail.com>
* @access public
* @package core
*/
/**
*	IMAGE HANDLING CLASS
*
*Class handles JPG images with GD2.0
*Series of functions make it easier to handle images. From simple transform to fit images, down to getting images from _POST
*are handled by this class. Extensions to it, will allow it to do more things. Once the library get's big, it should become
*a package on it's own.
*
* @version 1.0.0
* @package core
*/
class images{

	/**
	* Original image Reference object
	* @var ImageResource
	*/
	var $orgImage = "";
	/**
	* Origial width
	* @var Integer
	*/
	var $orgW = 0;
	/**
	* Original Height
	* @var Integer
	*/
	var $orgH = 0;
	/**
	* New temporary image handle
	* @var ImageResource
	*/
	var $tmpImage = "";
	/**
	* Ammount of compression to use when saving the image
	* @var Integer
	*/
	var $jpgCompression=90;

	/**
	*	Constructor class - checks to see if we have all defined variables loaded
	**/
	function images(){

	}
	/**
	*	Open the image and sets the initial values of the class
	*	@return void
	*	@param String $srcImage Absolute physical path to the image.
	**/
	function openImage($srcImage){
		$this->orgImage = imagecreatefromjpeg($srcImage);
		$this->orgW 	= imagesx($this->orgImage);
		$this->orgH 	= imagesy($this->orgImage);
	}


	/**
	*	Convert a Hexadecimal value of a colour to it's integer
	*	@return Array(r,g,b)
	*	@param String $hex The hexadecimal value RRGGBB
	*	@access private
	**/
	private function colordecode($hex){
		$code['r'] = hexdec(substr($hex, 0 ,2));
		$code['g'] = hexdec(substr($hex, 2 ,2));
		$code['b'] = hexdec(substr($hex, 4 ,2));
		return $code;
	} // end func colordecode

	/**
	*	Fetch the image submitted via post and save it to a known destination -
	*	optionally it may be left open to further edit the picture.
	*	@return Array/Boolean
	*@param String $formElement The name of the "file input" from the form where we can get the file.
	*@param String $destinationPath_and_Name The path and name of the file where we want to save this image.
	*@param Boolean $open Maintain the image open for post-processing after we've got it.
	*/
	function getImageFromPost($formElement,$destinationPath_and_Name,$open=true){
		$error = false;
		$errorDesc = "";

		$ext_accepted = array(".jpg");

		if(!in_array(strtolower(strrchr($_POST[$formElement]['name'], "." )), $ext_accepted)) {

			$error=true;
			$errorDesc="wrong type of extension";

		} elseif ($_POST[$formElement]['size']>IMAGE_MAX_SIZE) {
			$error=true;
			$errorDesc="wrong type of extension";
		} else {
			$img_name				= $destinationPath_and_Name;
			$img_tmp				= $_POST[$formElement]['tmp_name'];
			$img_destination		= DIR_IMAGE . $img_name;
			copy($img_tmp,$img_destination);
		}
		if ($error){
			return array("erro"=>$error,"desc"=>$errorDesc);
		}else{
			if ($open){
				$this->openImage($img_destination);
			}
			return true;
		}
	}
	/**
	*	Set the JPG Compression
	*	@return Void
	*@param Integer $val The Value of compression to set (can be from 0-10 or 0 - 100)
	*/
	function setCompression($val=70){
		if ($val>0 && $val<10){
			$val=10*$val;
		}elseif ($val>100){
			$val=100;
		}elseif ($val<0){
			$val=0;
		}
		$this->jpgCompression=$val;
	}

	/**
	*Resize the image - Using copy resized may cause a loss of quality, but it may solve some problems with compatability issues
	* in certain servers. Whenever possible use the copyResampled. The function attempts to identify if the function is available
	* but in some cases it may be better to comment out the if and leave only the copyResized statement.
	*	@return Void
	*@param Integer $width Width of the new image
	*@param Integer $height Height of the new image
	*/
	function resizeImage($width,$height){
		//$width++;
		//$height++;
		$dst_img	= imagecreatetruecolor($width,$height);
		if (!is_callable("imagecopyresampled")){
			// Maintained in here for compatibility issues
			imagecopyresized($dst_img,$this->orgImage,0,0,0,0,$width,$height,$this->orgW,$this->orgH);
		}else{
			imagecopyresampled($dst_img,$this->orgImage,0,0,0,0,$width,$height,$this->orgW,$this->orgH);
		}
		$this->orgImage = $dst_img;
	}

	/**
	*Resize the image so that it fits within the maximum values. For example, if you were to scale an image 300x100 into a
	*maximum 30x30 image, you'd end up with a 30x10 image.
	*@return Void
	*@param Integer $width Maximum width of the new image
	*@param Integer $height Maximum height of the new image
	*/
	function transformToFit($newX,$newY){
		$x=$this->orgW;
		$y=$this->orgH;
		$mlt=$newX/$x;
		$nx=ceil($x * $mlt);
		$ny=ceil($y * $mlt);

		if ($ny>$newY){
			$mlt=$newY/$ny;
			$nx=ceil($nx * $mlt);
			$ny=ceil($ny * $mlt);
		}
		$this->resizeImage($nx,$ny);
	}

	/**
	*Resize the image so that it fits within the maximum values and add a background colour
	*to fill in the rest of the image.
	*For example, if you want to scale an image of 300x100 into a square of 30x30, you'd end up with an image file
	*30x30 where the original image would have 30x10 and a background colour would fill the rest
	*	@return Void
	*@param Integer $width Maximum width of the new image
	*@param Integer $height Maximum height of the new image
	*@param String $bgColor Hexadecimal representation of the colour to fill the image with.
	*/

	function scaleInto($newX,$newY,$bgColor="FFFFFF"){
		$mlt =($this->orgH > $this->orgW)?$newY/$this->orgH:$newX/$this->orgW;

		$backgroundimage = imagecreatetruecolor($newX,$newY);
		$code = $this->colordecode($bgColor);
		$backgroundcolor = ImageColorAllocate($backgroundimage, $code[r], $code[g], $code[b]);
		ImageFilledRectangle($backgroundimage, 0, 0, $newX, $newY, $backgroundcolor);
		$x = imagesx($this->orgImage);
		$y = imagesy($this->orgImage);

		ImageAlphaBlending($backgroundimage, true);
		$this->transformToFit($newX,$newY);

		$x = imagesx($this->orgImage)+1;
		$y = imagesy($this->orgImage)+1;
		$sX = ceil(($newX-$x)/2);
		$sY =ceil(($newY-$y)/2);

		imagecopy($backgroundimage, $this->orgImage, $sX, $sY, 0, 0, $newX+4, $newY+4);
		// fix right side
		ImageFilledRectangle($backgroundimage, ($newX-$sX-1), 0, $newX, $newY, $backgroundcolor);

		// fix bottom side
		ImageFilledRectangle($backgroundimage, 0, ($newY-$sY-1), $newX, $newY, $backgroundcolor);

		$this->orgImage = $backgroundimage;

	}
	/**
	*Add a stroke to the image (stroke is a border). The function will add a border of any colour to the inside of the image.
	*@return Void
	*@param Integer $strokeWidth Width in pixels fo the stroke
	*@param String $strokeColor The hexadecimal representation of the colour to stroke with. (FF6600)
	*/
	function strokeImage($strokeWidth, $strokeColor="FFFFFF"){
		$width = imagesx($this->orgImage);
		$height = imagesy($this->orgImage);
		$code = $this->colordecode($strokeColor);
		$color = ImageColorAllocate($this->orgImage, $code['r'], $code['g'], $code['b']);
		if ($strokeWidth>1){
			for ($i=0;$i<$strokeWidth;$i++){
				ImageRectangle($this->orgImage, $i, $i, $width-($i+1), $height-($i+1), $color);
			}
		}else{
			ImageRectangle($this->orgImage, 0, 0, $width-1, $height-1, $color);
		}
	}
	/**
	*Save the current image to disk using the current applied JPG compression settings.
	*	@return Void
	*@param String $destImage The absolute path and name of the image to be saved.
	*/
	function saveImage($destImage){
		imagejpeg($this->orgImage, $destImage, $this->jpgCompression);
	}

}


?>
